package com.lry.basic.algorithm.graph;

import java.util.Scanner;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/8/7
 */
public class MatrixGraph {
    public static Graph graph;
    static int[] visit;
    public MatrixGraph(char []vertex,int arcNum){
        if(null==vertex||vertex.length==0||arcNum<=0)return;
        graph = new Graph();
        graph.vertex = vertex;//顶点集合
        graph.arcNum = arcNum;//边条数
        int vertexNum = vertex.length;
        int[][] arc = new int[vertexNum][vertexNum];
        Scanner scanner = new Scanner(System.in);
        for(int k=0;k<arcNum;k++){//初始化边
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            int wight = scanner.nextInt();
            arc[i][j]= wight;
        }
        graph.arc = arc;
    }
    public static void main(String[] args) {
        MatrixGraph mGraph = new MatrixGraph(new char[]{'A','B','C','D'},4);
        print(graph.arc);
        visit = new int[graph.vertex.length];
        dfs(0);
    }

    public static void dfs(int v){
        System.out.print(graph.vertex[v]+" ");
        visit[v] = 1;
        for(int j=0;j<graph.vertex.length;j++){
            if(graph.arc[v][j]!=0&&visit[j]!=1)
                dfs(j);
        }
    }
    public static void print(int [][]arc){
        for(int i=0;i<arc.length;i++){
            for(int j=0;j<arc[0].length;j++){
                System.out.print(arc[i][j]+" ");
            }
            System.out.println();
        }
    }
    class Graph{
        char [] vertex;//顶点数组
        int [][] arc;//边数组
        int arcNum;//边条数
    }
}
